package com.example.android.utils

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.example.android.R

/**
 * @ClassName MyDialog
 * @Author Ryan Kamanri
 * @Date 2022/6/1
 * @Description 对话框封装
 **/
class MyDialog(context: Context): Dialog(context) {

    private lateinit var _View: View

    fun SetOnSureButton(text: String = "确认", onSure: () -> Unit): MyDialog {
        val sureButton = _View.findViewById<Button>(R.id.dialog_btn_sure)
        sureButton.text = text
        sureButton.setOnClickListener {
            onSure()
        }
        return this
    }

    fun SetOnCancelButton(text: String = "取消", onCancel: () -> Unit): MyDialog {
        val cancelButton = _View.findViewById<Button>(R.id.dialog_btn_cancel)
        cancelButton.text = text
        cancelButton.setOnClickListener {
            onCancel()
        }
        return this
    }

    fun SetTitle(title: String): MyDialog {
        val titleView = _View.findViewById<TextView>(R.id.dialog_title)
        titleView.text = title
        return this
    }

    fun SetContent(content: String): MyDialog {
        val contentView = _View.findViewById<TextView>(R.id.dialog_content)
        contentView.text = content
        return this
    }

    @SuppressLint("InflateParams")
    override fun create() {
        super.create()
        _View = LayoutInflater.from(context).inflate(R.layout.app_dialog, null)
    }

    override fun show() {
        super.show()
        window?.setContentView(_View)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    }
}